package com.sks.web.action.district;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.pojo.Building;
import com.sks.service.house.AreaService;
import com.sks.service.house.BuildingService;
import com.sks.service.house.HouseService;
import com.sks.service.house.HouseTypeService;
import com.sks.service.house.MetroService;
import com.sks.service.house.MetroStationService;
import com.sks.service.house.PropertyTypeService;
import com.sks.service.uploadfile.UploadFileService;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.house.BuildingForm;
/**
 * 主要功能为返回小区列表
 * @author yuanjiayuan
 *
 */
@Controller("/sks/district")
public class DistrictAction extends BasicAction{
	
	@Resource(name = "areaServiceBean") private AreaService areaService;
	@Resource(name = "metroStationServiceBean") private MetroStationService metroStationService;
	@Resource(name = "metroServiceBean") private MetroService metroService;
	@Resource(name = "houseServiceBean") private HouseService houseService;
	@Resource(name = "uploadFileServiceBean")private UploadFileService uploadFileService;
	@Resource(name = "buildingServiceBean")	private BuildingService buildingService;
	@Resource(name = "propertyTypeServiceBean") private PropertyTypeService propertyTypeService;
	@Resource(name = "houseTypeServiceBean") private HouseTypeService houseTypeService;

	

	
	/**
	 * 返回楼盘需要的对象
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		setBaseData(request);

		BuildingForm formbean = (BuildingForm) form;
		StringBuffer sb = new StringBuffer(" o.status = '"
				+ Building.BUILDING_STATUS_PUBLISHED + "'"); // 只查询已发布的
		List<Integer> params = new ArrayList<Integer>();

		if (null != formbean.getTopAreaId()) {
			if (null == formbean.getAreaId()) {
				sb.append(" and o.area.parentArea.areaId = ?");
				params.add(formbean.getTopAreaId());
			} else {
				sb.append(" and o.area.areaId = ?");
				params.add(formbean.getAreaId());
			}
		}

		if (null != formbean.getMetroId()) {
			sb.append(" and o.metroStation.metroByMetroId.metroId = ?");
			params.add(formbean.getMetroId());
		}

		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		if (null != formbean.getOrderBy()) {
			String[] ss = formbean.getOrderBy().split("_");
			if (ss.length >= 2) {
				orderby.put(ss[0], ss[1]);
			}
		}
		orderby.put("recommend", "desc");

		PageView<Building> pv = new PageView<Building>(12, formbean.getPage());
		pv.setQueryResult(buildingService.getScrollData(pv.getFirstResult(),
				pv.getMaxresult(), sb.toString(), params.toArray(), orderby));

		StringBuffer house_saleN = new StringBuffer(
				" o.transaStatus = 1  and o.building.buildingId = ?"); // 查询出售的房源个数
		StringBuffer house_rentN = new StringBuffer(
				" o.transaStatus <> 1 and o.building.buildingId = ?"); // 查询出租的房源个数
		params.clear(); // 清空参数
		for (Building building : pv.getRecords()) {
			params.add(building.getBuildingId());
			int saleNum = houseService
					.getScrollData(0, 9999999, house_saleN.toString(),
							params.toArray()).getResultlist().size();// 计算出小区的出售房源数量
			int rentNum = houseService
					.getScrollData(0, 9999999, house_rentN.toString(),
							params.toArray()).getResultlist().size();// 计算出小区的出租房源数量
			building.setRent(rentNum);
			building.setSale(saleNum);
			params.clear();
		}
		request.setAttribute(PAGEVIEW, pv);
		return mapping.findForward(LIST);
	}
	

	/**
	 * 初始化基础数据
	 * @param request
	 */
	private void setBaseData(HttpServletRequest request) {
		      request.setAttribute("areaPageView", areaService.getScrollData().getResultlist());
		      request.setAttribute("metroStationPageView", metroStationService.getScrollData().getResultlist());
		      request.setAttribute("metroPageView", metroService.getScrollData().getResultlist());
		      request.setAttribute("uploadFilePageView", uploadFileService.getScrollData().getResultlist());
		      request.setAttribute("houseTypes", houseTypeService.getScrollData().getResultlist());
		      request.setAttribute("topAreas", areaService.findAreas(true, true));
		      request.setAttribute("propertyTypes", propertyTypeService.getScrollData().getResultlist());
	}
	
	
}
